﻿using BlizzardApi.Services;
using FluentNHibernate;
using FluentNHibernate.Cfg.Db;
using Model;
using NHibernate;
using NHibernate.Cfg;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace Command.Framework
{
	public class CommandRegistry : Registry
	{
		public CommandRegistry()
		{
			For<IWebRequestor>()
				.Use<WebRequestor>()
				.Ctor<string>("baseUrl")
				.Is("http://us.battle.net/");

			For<ISession>()
				.Use(SessionFactory);

			For<Configuration>()
				.Use(ConfigurationFactory);
		}

		private static ISession SessionFactory(IContext context)
		{
			var config = context.GetInstance<Configuration>();
			var sessionFactory = config.BuildSessionFactory();
			return sessionFactory.OpenSession();
		}

		private static Configuration ConfigurationFactory()
		{
			Configuration config = MsSqlConfiguration
				.MsSql2008
				.ConnectionString(c => c.Is("Server=(local);Database=gdb;User=pub;Password=pub"))
				.ConfigureProperties(new Configuration());
			var models = new PersistenceModel();
			models.AddMappingsFromAssembly(typeof(Guild).Assembly);
			models.Configure(config);
			return config;
		}
	}
}
